

$(function(){
	$("#yuyue").hide();
	$("#reserveTime").attr("checked",false);
	
	$("#ydate").focus(function(){
		$(this).select();
		$("#yhour").val('');
		$("#yminu").val('');
		showDate();
	});
	$("#ydate").blur(function(){
		setTimeout("closeDate()",200);
	});
	$("#yhour").focus(function(){
		$(this).select();
		$("#yminu").val('');
		showHour();
	});
	$("#yhour").blur(function(){
		setTimeout("closeHour()",200);
	});
	$("#yminu").focus(function(){
		$(this).select();
		showMinu();
	});
	$("#yminu").blur(function(){
		setTimeout("closeMinu()",200);
	});
	
});


//提示框
var msgLeft,msgTop;
var times ='';
var isEasyOrder='';//在所选区的当天服务开始时间之前isEasyOrder不为undefined

var s_hour_minu_b;	//最早时间字符hhmm
var s_hour_minu_e;	//最晚时间字符hhmm
var s_hour_b;		//最早时间字符hh
var s_hour_e;		//最晚时间字符hh
var hour_b;			//最晚时间数字hh
var hour_e;			//最晚时间数字hh

function showDate(){
	var orderFlag=currentIsOrderEasy();
	var isOrder = orderFlag.isOrder;
	//判断是不是当前时间是是不是比正常上班收件时间早
	 isEasyOrder = orderFlag.earlyBeginTime;
	getAbsolutePos(document.getElementById("ydate"));
	var obj = document.createElement("div");
	obj.id = "dateDiv";
	obj.className = "timeSubbox";
	obj.style.cssText="width:"+160+"px"+";left:"+msgLeft+"px;"+"top:"+msgTop+
	 "px;";
	var cityId = $("#s_cityId").val();
	if(cityId == undefined || cityId == ''){
		cityId = $("#send_cityId").text();
	}
	if(cityId == undefined || cityId == ''){
		cityId = "default";
	}
	
	syncCallService('/service/order/order/reserveDay/' + cityId, 'get', 'JSON', '', function(data){
		if(data != null){
			times=data;
		}
	}, function(){
	});

	var timeHTML ="";
   //将获取到的时间区分，天、小时、分钟	
	if(!isOrder){
		if(undefined !=isEasyOrder){

			for(var i=0;i<times.length;i++)
			{
				if(times[i] != null && times[i] != '')
				{
					var timeDay = times[i].split(" ")[0];
					timeHTML +='<a class="date" onclick="javascript:dateValue('+ "'" +timeDay+"'"+');">' + timeDay + '</a>';
				}
			}
		}else{
			for(var i=1;i<times.length;i++)
			{
				if(times[i] != null && times[i] != '')
				{
					var timeDay = times[i].split(" ")[0];
					timeHTML +='<a class="date" onclick="javascript:dateValue('+ "'" +timeDay+"'"+');">' + timeDay + '</a>';
				}
			}
		}
	}else{
		for(var i=0;i<times.length;i++)
		{
			if(times[i] != null && times[i] != '')
			{
				var timeDay = times[i].split(" ")[0];
				var timeHm = times[i].split(" ")[1];//0900-1910
				var timeHour = parseInt(timeHm.split("-")[1].substring(0,2),10);
				var timeMinu = parseInt(timeHm.split("-")[1].substring(2,4),10);
				var d = new Date().format('yyyy/MM/dd');
				var h = parseInt(new Date().getHours(),10);
				var minu = parseInt(new Date().format('mm'),10);
				if(d == timeDay){//如果在当天时间，在结束下单时间超过45分钟，不显示当天的预约时间。例如：截单时间1955
					minu=minu+15;
					if(minu > 60){
						h=h+1;
						minu=minu-60;
					}

					var s_m = minu<10?("0"+minu):minu;
					var s_h = h<10?("0"+h):h;
					//今天
					var hm = s_h +""+ s_m;
					var hm_e = timeHour+""+timeMinu;
					if(hm>=hm_e){
						continue;						
					}
				}
				timeHTML +='<a class="date" onclick="javascript:dateValue('+ "'" +timeDay+"'"+');">' + timeDay + '</a>';
			}
		}
	}
	

	obj.innerHTML=timeHTML;
	 document.body.appendChild(obj);
}
function closeDate(){
	$("#dateDiv").remove();
}

function dateValue(day){
	$("#ydate").val(GetDateStr(day));
	closeDate();
	$('#yhour').focus();
}
function showHour(){
	var time=getChooseTime();
	if(time==""){
		sfAlert(i18n.order_Day_reserve_time);
		return;
	}
	getCompareTimeStr(time);
	//小时
	var rows ='';
	afterTime = s_hour_e;
	for ( var i = hour_b; i <= hour_e; i++) {
		if(i<10){
			rows += '<a class="date" onclick="hourValue('+("0"+i)+')">'+("0"+i)+'</a>';
		}else{
			rows += '<a class="date" onclick="hourValue('+i+')">'+i+'</a>';
		}
		
	}
	closeHour();
	getAbsolutePos(document.getElementById("yhour"));
	var obj = document.createElement("div");
	obj.id = "hourDiv";
	obj.className = "timeSubbox";
	obj.style.cssText="width:"+190+"px"+";left:"+msgLeft+"px;"+"top:"+msgTop+
	 "px;";
	obj.innerHTML= rows;
	 document.body.appendChild(obj);
}
function closeHour(){
	$("#hourDiv").remove();
}

function hourValue(hour){
	$("#yhour").val(hour);
	$('#yminu').focus();
	closeHour();
}
function showMinu(){
	closeMinu();
	var ling = "'00'";
	getAbsolutePos(document.getElementById("yminu"));
	var obj = document.createElement("div");
	obj.id = "minuDiv";
	obj.className = "timeSubbox";
	obj.style.cssText="width:"+160+"px"+";left:"+msgLeft+"px;"+"top:"+msgTop+
	 "px;";
	
	var time=getChooseTime();
	if(time==""){
		sfAlert(i18n.order_Day_reserve_time);
		return;
	}
	getCompareTimeStr(time);

	var rows = ''; 
	//选中时间
	var ht = $("#yhour").val();
	var hts = (parseInt(ht));
	var s_ht = hts<10?("0"+hts):hts;
	//今天
	var hm00 = s_ht + '00';
	if(hm00 >= s_hour_minu_b && hm00 <= s_hour_minu_e){
			rows+='<a class="date" onclick="minuValue('+ling+');">00</a>';
	}
//	var hm15 = s_ht + '15';
//	if(hm15 >= s_hour_minu_b && hm15 <= s_hour_minu_e){
//		rows+='<a class="date" onclick="minuValue(15);">15</a>';
//	}
	var hm30 = s_ht + '30';
	if(hm30 >= s_hour_minu_b && hm30 <= s_hour_minu_e){
		rows+='<a class="date" onclick="minuValue(30);">30</a>';
	}
//	var hm45 = s_ht + '45';
//	if(hm45 >= s_hour_minu_b && hm45 <= s_hour_minu_e){
//		rows+='<a class="date" onclick="minuValue(45);">45</a>';
//	}
	if(rows == ''){
		return;
	}

	obj.innerHTML= rows;

	document.body.appendChild(obj);
}
function closeMinu(){
	$("#minuDiv").remove();
}
function minuValue(minu){
	$("#yminu").val(minu);
	closeMinu();
}

function getAbsolutePos(obj)
{
   var t=obj.offsetTop;
   var l=obj.offsetLeft;
   while(obj=obj.offsetParent)
   {
       t+=obj.offsetTop;
       l+=obj.offsetLeft;
   }
   msgTop = t+28;
   msgLeft = l;
}
function GetDateStr(dayTime) {
   //时间格式：2013/11/20
    var dayt =dayTime.split("/");
    var y =dayt[0];
    var m =null;
    var d=null;
    if(dayt[1].length <2){
  	  m = "0" + dayt[1];
    }else
    {
	   m=dayt[1];
    }
    if(dayt[2].length <2)
    {
	    d = "0" + dayt[2];
    }else
    {
  	  d=dayt[2];
    }
  

  return y+"-"+m+"-"+d;
}



$("#reserveTime").click(function(){
    var reserveTime = $('input:checkbox[name="reserveTime"]:checked').val();
    if(reserveTime == undefined){
    	$("#yuyue").hide();
    }else{
    	$("#yuyue").show();
    }
});
function getCompareTimeStr(time){
	var currentTime = new Date().format('yyyy-MM-dd');
	var hour_minu_n =  new Date().format('hhmm');
	var ydate = $("#ydate").val();
	
	var hour_minu_b =time.split("-")[0];
	var hour_minu_e =time.split("-")[1];
	if(currentTime == ydate){//今天
		hour_minu_b=hour_minu_b<hour_minu_n?hour_minu_n:hour_minu_b;
	}

	hour_b = parseInt(hour_minu_b.substring(0,2),10);
	var minu_b = parseInt(hour_minu_b.substring(2,4),10);
	if(minu_b>45){
		hour_b = hour_b+1;
		minu_b =0;
	}
	s_hour_b=hour_b<10?("0"+hour_b):hour_b;
	var s_minu_b=minu_b<10?("0"+minu_b):minu_b;
	s_hour_minu_b=s_hour_b+''+s_minu_b;

	hour_e = parseInt(hour_minu_e.substring(0,2),10);
	var minu_e = parseInt(hour_minu_e.substring(2,4),10);
	
	s_hour_e=hour_e<10?("0"+hour_e):hour_e;
	var s_minu_e=minu_e<10?("0"+minu_e):minu_e;
	s_hour_minu_e=s_hour_e+''+s_minu_e;
}

function getChooseTime(){
	var daySel = $("#ydate").val();//2013-11-28
	if(daySel == undefined || daySel == ''){
		return "";
	}
	var daySelStr = daySel.replace(/-/g,"/");// 2013/11/28
	var time ='';
	var timeDay='';
	//将获取到的时间区分，小时、分钟	
	for(var i=0;i<times.length;i++)
	{
		if(times[i] != null && times[i] != '')
		{
			timeDay = times[i].split(" ")[0];
			if(timeDay==daySelStr)
			{
			   time = times[i].split(" ")[1];//0000-0000
			}
		}
	}
	return time;
}
